package com.mzj.saas.mservice.propertybill.controller;

import com.mzj.saas.commons.ApiData;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.bill.vo.SaveExpenseBillVOAndSurrenderVO;
import com.mzj.saas.mservice.community.vo.HouseSurrenderVO;
import com.mzj.saas.mservice.house.vo.HouseVO;
import com.mzj.saas.mservice.propertybill.service.WithDrawService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/withDraw")
public class WithDrawController {
    @Autowired
    private WithDrawService withDrawService;

    /**
     * 退租管理列表
     * pk
     * @param accessToken
     * @param pageNumber
     * @param pageSize
     * @param vo
     * @param houseIdList
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultBean<ApiData<HouseVO>> list(@RequestHeader String accessToken,
                                            @RequestParam(defaultValue = "0") Integer pageNumber,
                                            @RequestParam(defaultValue = "10") Integer pageSize,
                                            HouseVO vo, @RequestParam(required = false) List<String> houseIdList){
        return withDrawService.list(accessToken,pageNumber,pageSize,vo,houseIdList);
    }

    /**
     * 验房
     * ok
     * @param accessToken
     * @param
     * @return
     */
    @PostMapping("/homeInspection")
    @ResponseBody
    public ResultBean<ApiData<Object>> homeInspection(@RequestHeader String accessToken, @RequestBody HouseSurrenderVO vo){
        return withDrawService.homeInspection(accessToken,vo);
    }

    /**
     *查验合同
     * ok
     */
    @GetMapping("/checkTheContract")
    @ResponseBody
    public ResultBean<ApiData<HouseSurrenderVO>>  checkTheContract(@RequestHeader String accessToken,Long id ,String houseId) {
        return withDrawService.checkTheContract(accessToken,id,houseId);
    }

    /**
     * 退租账单 生成退费记录 并将未缴费状态账单变更状态
     */
    @PostMapping("/saveLeasebackBill")
    @ResponseBody
    public ResultBean saveLeasebackBill(@RequestHeader String accessToken, @RequestBody SaveExpenseBillVOAndSurrenderVO vo){
        return withDrawService.saveLeasebackBill(accessToken,vo);
    }

    /**
     * 点击确认
     */
    @PostMapping("/determine")
    @ResponseBody
    public ResultBean determine(@RequestHeader String accessToken,@RequestBody HouseSurrenderVO vo){
        return withDrawService.determine(accessToken,vo);
    }


    /**
     * 点击已完成  修改退租状态，并且调用删除同住人的接口  清理房屋合同期限信息
     */
    @GetMapping("/finishDraw")
    @ResponseBody
    public ResultBean finishDraw(@RequestHeader String accessToken,@RequestParam(value = "id")Long id,@RequestParam(value = "houseId")String houseId) {
        return withDrawService.finishDraw(accessToken,id,houseId);
    }

    /**
     * 下载账单
     * 退租id，账单id
     */
    @GetMapping("/loadBill")
    public void loadBill(Long billId, HttpServletResponse response) {
        try {
            withDrawService.loadBill(billId,response);
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            log.error(e.getMessage(), e);
        }
    }
}
